﻿using Dapper;
using S_Userinfor.Domains;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace S_Gridding.Infrastructure.Dapper
{
    /// <summary>
    /// Dapper的底层仓储实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DapperRepository<T> : IDapperRepository<T> where T : class
    {
        public DapperContext _dapper;

        public DapperRepository(DapperContext dapper)
        {
            _dapper = dapper;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="selectOneSql"></param>
        /// <returns></returns>
        public async Task<T> GetOne(int Id, string selectOneSql)
        {

            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    var result = await dbConnection.QueryFirstOrDefaultAsync<T>(selectOneSql, new { Id = Id });
                    return result;
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 显示加分页
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="selectSql"></param>
        /// <returns></returns>
        public async Task<List<T>> SelectAsync(int PageSize, string selectSql)
        {
            IDbConnection dbConnection = _dapper.Command;
            {
                try
                {
                    if (dbConnection.State == ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }

                    return await Task.Run(() => dbConnection.Query<T>(selectSql, new { PageSize = PageSize }).ToList());
                }
                finally
                {
                    if (dbConnection.State == ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }


        /// <summary>
        /// 树形
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="selectTreeSql"></param>
        /// <returns></returns>
        public Task<List<T>> GetTree(int pid, string selectTreeSql)
        {
            IDbConnection dbConnection= _dapper.Command;
            {
                try
                {
                    if(dbConnection.State== ConnectionState.Closed)
                    {
                        dbConnection.Open();
                    }
                    return Task.Run(() => dbConnection.Query<T>(selectTreeSql, new { pid }).ToList());
                }
                finally
                {
                    if (dbConnection.State==ConnectionState.Open)
                    {
                        dbConnection.Close();
                    }
                }
            }
        }
    }
}
